package com.example.litterpainterdemo.utils

import android.graphics.Bitmap
import android.graphics.Path
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.BufferedOutputStream
import java.io.File
import java.io.FileOutputStream

object FileUtils {

    suspend fun saveBitmap(bitmap: Bitmap):String = withContext(Dispatchers.IO){
        val timeName = TimeUtils.timeName()
        val path = "${ContextHelper.getContext.get()!!.filesDir}/${timeName}"

        //写入硬盘
        BufferedOutputStream(FileOutputStream(path)).use {
            bitmap.compress(Bitmap.CompressFormat.JPEG,100,it)
        }
        //返回路径
        path
    }
    suspend fun deleteFile(path: String) = withContext(Dispatchers.IO){
        val file = File(path)
        if(file.exists()){
            //删除
            file.delete()
        }
    }
}